package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.*;
import com.ruoyi.yipaiju.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/client")
public class ClientController extends BaseController {

    @Autowired
    private IForeclosedHouseService foreclosedHouseService;
    @Autowired
    private IDiscountHouseService discountHouseService;
    @Autowired
    private IForeclosedHouseBookingorderService foreclosedHouseBookingorderService;
    @Autowired
    private IDiscountHouseBookingorderService discountHouseBookingorderService;
    @Autowired
    private IHousePreuploadOrderService housePreuploadOrderService;
    @Autowired
    private IInvestigationOrderService investigationOrderService;
    @Autowired
    private IDemandOrderService demandOrderService;
    @Autowired
    private IValueAddedOrderService valueAddedOrderService;
    @Autowired
    private IForeclosedHouseSigningorderService foreclosedHouseSigningorderService;
    @Autowired
    private IDiscountHouseSigningorderService discountHouseSigningorderService;

    @Autowired
    private IFavoriteService favoriteService;
    @Autowired
    private INoticeService noticeService;

    @GetMapping("/houses/foreclosed/{id}")
    public ApiResult housesForeclosed(@PathVariable("id") Long id)
    {
        ForeclosedHouse foreclosedHouse = foreclosedHouseService.selectForeclosedHouseById(id);
        //获取用户是否收藏
        Favorite favorite = favoriteService.selectOneFavorite(Builder.of(Favorite::new)
                .with(Favorite::setClient, SecurityUtils.getUserId().toString())
                .with(Favorite::setForeclosedHouseId, id)
                .build());
        if (favorite == null){
            foreclosedHouse.setIsfavor(0);
        }else {
            foreclosedHouse.setIsfavor(1);
            foreclosedHouse.setFavorId(favorite.getId());
        }
        Notice notice = noticeService.selectOneNotice(Builder.of(Notice::new)
                .with(Notice::setClient, SecurityUtils.getUserId().toString())
                .with(Notice::setForeclosedHouseId, id)
                .build());
        if (notice == null){
            foreclosedHouse.setIsnotice(0);
        }else {
            foreclosedHouse.setIsnotice(1);
            foreclosedHouse.setNoticeId(notice.getId());
        }
        return ApiResult.success(foreclosedHouse);
    }

    @GetMapping("/houses/discount/{id}")
    public ApiResult housesDiscount(@PathVariable("id") Long id)
    {
        DiscountHouse discountHouse = discountHouseService.selectDiscountHouseById(id);
        if (discountHouse != null){
            //获取用户是否收藏
            Favorite favorite = favoriteService.selectOneFavorite(Builder.of(Favorite::new)
                    .with(Favorite::setClient, SecurityUtils.getUserId().toString())
                    .with(Favorite::setDiscountHouseId, id)
                    .build());
            if (favorite == null){
                discountHouse.setIsfavor(0);
            }else {
                discountHouse.setIsfavor(1);
                discountHouse.setFavorId(favorite.getId());
            }
            Notice notice = noticeService.selectOneNotice(Builder.of(Notice::new)
                    .with(Notice::setClient, SecurityUtils.getUserId().toString())
                    .with(Notice::setDiscountHouseId, id)
                    .build());
            if (notice == null){
                discountHouse.setIsnotice(0);
            }else {
                discountHouse.setIsnotice(1);
                discountHouse.setNoticeId(notice.getId());
            }
            return ApiResult.success(discountHouse);
        }else {
            return ApiResult.error("房源不见了...");
        }
    }

    /**
     * 预约订单完成评价
     * @param id
     * @return
     */
    @PostMapping("/houses/foreclosed/booking/orders/{id}")
    public ApiResult housesForeclosedBookingOrders(@PathVariable("id") Long id, @RequestBody ForeclosedHouseBookingorder foreclosedHouseBookingorder){
        foreclosedHouseBookingorder.setId(id);
        foreclosedHouseBookingorder.setStage(4);
        int result = foreclosedHouseBookingorderService.updateForeclosedHouseBookingorder(foreclosedHouseBookingorder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 折扣订单完成评价
     * @param id
     * @return
     */
    @PostMapping("/houses/discount/booking/orders/{id}")
    public ApiResult housesDiscountBookingOrders(@PathVariable("id") Long id, @RequestBody DiscountHouseBookingorder discountHouseBookingorder){
        discountHouseBookingorder.setId(id);
        discountHouseBookingorder.setStage(4);
        int result = discountHouseBookingorderService.updateDiscountHouseBookingorder(discountHouseBookingorder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 放盘订单完成评价
     * @param id
     * @return
     */
    @PostMapping("/houses/preupload/orders/{id}")
    public ApiResult housesPreuploadOrders(@PathVariable("id") Long id, @RequestBody HousePreuploadOrder housePreuploadOrder){
        housePreuploadOrder.setId(id);
        housePreuploadOrder.setStage(4);
        int result = housePreuploadOrderService.updateHousePreuploadOrder(housePreuploadOrder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 尽调订单完成评价
     * @param id
     * @return
     */
    @PostMapping("/houses/foreclosed/investigation/orders/{id}")
    public ApiResult housesForeclosedInvestigationOrders(@PathVariable("id") Long id, @RequestBody InvestigationOrder investigationOrder){
        investigationOrder.setId(id);
        investigationOrder.setStage(4);
        int result = investigationOrderService.updateInvestigationOrder(investigationOrder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 好房订单完成评价
     * @param id
     * @return
     */
    @PostMapping("/demand/orders/{id}")
    public ApiResult demandOrders(@PathVariable("id") Long id, @RequestBody DemandOrder demandOrder){
        demandOrder.setId(id);
        demandOrder.setStage(4);
        int result = demandOrderService.updateDemandOrder(demandOrder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 增值订单完成评价
     * @param id
     * @return
     */
    @PostMapping("/valueadded/orders/{id}")
    public ApiResult valueaddedOrders(@PathVariable("id") Long id, @RequestBody ValueAddedOrder valueAddedOrder){
        valueAddedOrder.setId(id);
        valueAddedOrder.setStage(4);
        int result = valueAddedOrderService.updateValueAddedOrder(valueAddedOrder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }


    /**
     * 优选签约完成评价
     * @param id
     * @param foreclosedHouseSigningorder
     * @return
     */
    @PostMapping("/houses/foreclosed/signing/orders/{id}")
    public ApiResult foreclosedOrders(@PathVariable("id") Long id, @RequestBody ForeclosedHouseSigningorder foreclosedHouseSigningorder){
        foreclosedHouseSigningorder.setId(id);
        foreclosedHouseSigningorder.setoStage(4);
        int result = foreclosedHouseSigningorderService.updateForeclosedHouseSigningorder(foreclosedHouseSigningorder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }


    /**
     * 折扣签约完成评价
     * @param id
     * @param discountHouseSigningorder
     * @return
     */
    @PostMapping("/houses/discount/signing/orders/{id}")
    public ApiResult discountOrders(@PathVariable("id") Long id, @RequestBody DiscountHouseSigningorder discountHouseSigningorder){
        discountHouseSigningorder.setId(id);
        discountHouseSigningorder.setoStage(4);
        int result = discountHouseSigningorderService.updateDiscountHouseSigningorder(discountHouseSigningorder);
        return result > 0 ? ApiResult.success() : ApiResult.error();
    }
}
